<?php
require('common_view_functions.php');
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Blog 用户后台管理 - 网志分类及 Tag</title>
<link rel="shortcut icon" href="/img/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/css/mgmt.css" />
<link rel="stylesheet" type="text/css" href="/css/3rd/jqModal.css" />
</head>
<body id="bodySettings">
<div id="wrapper">
	<!-- header -->
	<?php require('common_header.php'); ?>
	<!-- /header -->
	<div id="body">
		<div id="side">
			<ul id="settings_cates">
				<li><a href="<?php echo('/' . $blogger_uri . '/mgmt/settings'); ?>">基本设置 &raquo;</a></li>
				<li><a href="<?php echo('/' . $blogger_uri . '/mgmt/categories'); ?>" class="curr">网志分类与 Tag &raquo;</a></li>
				<li><a href="<?php echo('/' . $blogger_uri . '/mgmt/links'); ?>">友情链结 &raquo;</a></li>
			</ul>
		</div>
		<div id="main">
			<div id="catesDiv">
				<h3 class="sectionHeader">新增分类</h3>
				<form id="frmNewCate" name="frmNewCate">
					<label for="txtNewCateTitle">分类名：</label>
					<input id="txtNewCateTitle" name="txtNewCateTitle" class="textBox" maxlength="32" size="16" style="width:16em;" />
					<label for="txtNewCateDesc">分类简介：</label>
					<input id="txtNewCateDesc" name="txtNewCateDesc" class="textBox" maxlength="256" size="16" style="width:16em;" />
					<input type="button" id="btnAddNewCate" class="button" value="提交" />
				</form>
				<h3 class="sectionHeader">分类列表</h3>
				<dl id="catesListDl">Loading...</dl>
			</div>
			<div id="tagsDiv">
				<h3 class="sectionHeader">Tag 管理</h3>
				<select id="selTagList" class="textBox">
					<option value="-1" selected="selected">--- 选择一个 Tag ---</option>
					<?php foreach($tag_list as $tl) { ?>
					<option value="<?php echo($tl->tag_text); ?>"><?php echo(htmlspec($tl->tag_text) . ' (' . $tl->entry_num . ')'); ?></option>
					<?php } ?>
				</select>
				<label for="txtModTag">更名选中的 Tag：</label>
				<input type="text" id="txtModTag" class="textBox" maxlength="64" size="16" style="width:16em;" />
				<input type="button" id="btnModTag" class="button" value="确定" />
				<div><input type="button" id="btnDelTag" class="button" value="删除选中的 Tag" /></div>
			</div>
			<div class="z"></div>
		</div>
		<div class="z"></div>
	</div>
	<!-- footer -->
	<?php require('common_footer.php'); ?>
	<!-- /footer -->
</div>

<!-- JS -->
<script type="text/javascript" src="/js/3rd/jquery.pack.js"></script>
<script type="text/javascript" src="/js/3rd/jqModal.js"></script>
<script type="text/javascript" src="/js/3rd/json2.js"></script>
<script type="text/javascript">
<!--
var g_userSiteRoot = "<?php echo('/' . $blogger_uri); ?>";

var modFieldsCreated = false;
var modCateData = {
	"id": 0,
	"title": "",
	"desc": ""
};
var delCateData = {
	"id": 0
};

var newTagsString = "";

function purgeTagString(ts) {
	var ps = jQuery.trim(ts);
	ps = ps.replace(/\s{2,}/ig, " ");
	return ps;
}

function validateTags(tags) {
	var newTags = tags;
	if(newTags != "") {
		if(/[^\u4E00-\u9FA0a-zA-Z0-9._ ]/.test(newTags)) {
			alert("Tag 中不能含有除中文、字母、数字、句点及下划线之外的其他字符！");
			return false;
		}
		
		newTags = purgeTagString(newTags);
		var hasSame = false;
		var arrTags = newTags.split(" ");
		for(var i = 0; i < arrTags.length; i++) {
			for(var j = i + 1; j < arrTags.length; j++) {
				if(arrTags[i] == arrTags[j]) {
					hasSame = true;
					break;
				}
			}
		}
		if(hasSame) {
			alert("Tag 有重复，请修改！");
			return false;
		}
		newTagsString = newTags;
		return true;
	}
}

function addNewCateCallback(rdata) {
	$("#ajaxLoading").hide();
	
	$("#txtNewCateTitle").val("");
	$("#txtNewCateDesc").val("");
	
	rdata = rdata.split("_");
	
	if(rdata[0] == "SUCC") {
		loadCateList();
	} else {
		alert("Operation failed!");
		return;
	}
}

function modCateCallback(rdata) {
	$("#ajaxLoading").hide();
	
	rdata = rdata.split("_");
	if(rdata[0] != "SUCC") {
		alert("Operation failed!");
		return;
	}
	
	loadCateList();
}

function delCateCallback(rdata) {
	$("#ajaxLoading").hide();
	
	rdata = rdata.split("_");
	if(rdata[0] != "SUCC") {
		alert("Operation failed!");
		return;
	}
	
	var cid = delCateData.id;
	$("#dtc_" + cid).fadeOut();
	$("#ddc_" + cid).fadeOut();
}

function renameTagCallback(rdata) {
	$("#ajaxLoading").hide();
	
	rdata = rdata.split("_");
	if(rdata[0] != "SUCC") {
		alert("Operation failed!");
		return;
	}
	
	newTagsString = "";
	$("#txtModTag").val("");
	alert("更名成功。");
	location.reload();
}

function deleteTagCallback(rdata) {
	$("#ajaxLoading").hide();
	
	rdata = rdata.split("_");
	if(rdata[0] != "SUCC") {
		alert("Operation failed!");
		return;
	}
	
	alert("删除完毕。");
	location.reload();
}

function loadCateListCallback(rdata) {
	// edit category
	$("#catesDiv dl dt a").click(function() {
		var ac = $(this).attr("rel");
		ac = ac.split("_");
		var action = ac[0];
		var cid = ac[1];
		if(action == "e") {
			// show edit-box, spagehtti :(
			if(!modFieldsCreated) {
				modCateData.id = cid;
				modCateData.title = $("#dtc_" + cid + " span").text();
				modCateData.desc = $("#ddc_" + cid).text();
				var inpModTitle = "<input type=\"text\" id=\"txtModTitle\" class=\"textBox\" maxlength=\"32\" style=\"width:16em;\" value=\"" + modCateData.title + "\" />";
				var inpModDesc = "<input type=\"text\" id=\"txtModDesc\" class=\"textBox\" maxlength=\"256\" style=\"width:16em;\" value=\"" + modCateData.desc + "\" />";
				var inpModButtons = "<input type=\"button\" id=\"btnModCate\" class=\"button\" value=\"确定\" /> <input type=\"button\" id=\"btnModCateCancel\" class=\"button\" value=\"取消\" />"
				$("#dtc_" + cid + " span").html(inpModTitle);
				$("#ddc_" + cid).html(inpModDesc + " " + inpModButtons);
				modFieldsCreated = true;
				$("#dtc_" + cid + " div").hide();
				
				$("#btnModCateCancel").click(function() {
					$("#dtc_" + cid + " span").text(modCateData.title);
					$("#ddc_" + cid).text(modCateData.desc);
					modFieldsCreated = false;
					$("#dtc_" + cid + " div").show();
				});
				$("#btnModCate").click(function() {
					modCateData.title = $("#txtModTitle").val();
					modCateData.desc = $("#txtModDesc").val();
					// do cate-update
					$("#ajaxLoading").show();
					$.post(g_userSiteRoot + "/mgmt/categories", {
						"hidIsPostBack": "ja",
						"operation": "update",
						"posted_data": JSON.stringify(modCateData)
					}, modCateCallback);					
				});
			}
		} else if(action == "d") {
			// delete cate
			if(confirm("确定要删除该分类？（其下网志将归于“未分类”）")) {
				// do delete
				delCateData.id = cid;
				$("#ajaxLoading").show();
				$.post(g_userSiteRoot + "/mgmt/categories", {
					"hidIsPostBack": "ja",
					"operation": "delete",
					"posted_data": JSON.stringify(delCateData)
				}, delCateCallback);
			}
		}
	});
	
	modFieldsCreated = false;
	$("#ajaxLoading").hide();
}

function loadCateList() {
	$("#ajaxLoading").show();
	$("#catesListDl").load(g_userSiteRoot + "/mgmt/categories_list", {
		"R": Math.random()
	}, loadCateListCallback);
}

$(document).ready(function() {
	$("#btnAddNewCate").click(function() {
		var newCateTitle = $("#txtNewCateTitle").val();
		if(newCateTitle == "") {
			alert("分类名不能为空！");
			return;
		}
		var newCateDesc = $("#txtNewCateDesc").val();
		var newCateData = {
			"title": newCateTitle,
			"description": newCateDesc
		};
		
		$("#ajaxLoading").show();
		$.post(g_userSiteRoot + "/mgmt/categories", {
			"hidIsPostBack": "ja",
			"operation": "create",
			"posted_data": JSON.stringify(newCateData)
		}, addNewCateCallback);
	});
	
	$("#btnModTag").click(function() {
		var selTag = $("#selTagList").val();
		if(selTag != "-1") {
			var modTagText = $("#txtModTag").val();
			if(modTagText != "") {
				if(confirm("确实要更名此 Tag？")) {
					if(validateTags(modTagText)) {
						$("#ajaxLoading").show();
						$.post(g_userSiteRoot + "/mgmt/categories", {
							"hidIsPostBack": "ja",
							"operation": 'rename_tag',
							"tag_to_rename": selTag,
							"new_tag_string": newTagsString
						}, renameTagCallback);
					}
				}
			} else {
				alert("新的 Tag 不能为空！");
			}
		} else {
			alert("请先选择想要更名的 Tag！");
		}
	});
	
	$("#btnDelTag").click(function() {
		var selTag = $("#selTagList").val();
		if(selTag != "-1") {
			if(confirm("确定要删除此 Tag？（标有此 Tag 的网志不会被同时删除）")) {
				$("#ajaxLoading").show();
				$.post(g_userSiteRoot + "/mgmt/categories", {
					"hidIsPostBack": "ja",
					"operation": "delete_tag",
					"tag_to_delete": selTag
				}, deleteTagCallback);
			}
		}
	});
	
	loadCateList();
});
// -->
</script>
<!-- /JS -->

<div id="ajaxLoading"><img alt="Loading..." src="<?php echo(base_url() . 'img/ajax_loading.gif'); ?>" border="0" width="24" height="24" /></div>
</body>
</html>